package com.testing.inter;

import com.testing.common.AutoTools;
import com.testing.common.ExcelReader;
import com.testing.common.ExcelWriter;
import com.testing.common.Report;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @Classname InterTestNGDDT
 * @Description 类型说明
 * @Date 2022/11/14 22:06
 * @Created by 特斯汀Roy
 */
public class LoginInterTestNGDDT {
    InterKeyWord inter;
    String runDate;
    ExcelReader cases;
    ExcelWriter results;

    public static final int ASSERT_COL=0;
    public static final int KEY_COL=0;

    @BeforeClass
    public void initTest(){
        inter=new InterKeyWord();
        runDate = AutoTools.dateStr("yyyy-MM-dd HH：mm：ss");
        cases=new ExcelReader("Cases\\InterNGCases.xlsx");
        results=new ExcelWriter("Cases\\InterNGCases.xlsx",
                "Cases\\Result\\resultofInterNG"+runDate+".xlsx");
    }

    @Test(dataProvider = "interData")
    public void runInterDDT(String rowNO,String catagory,String casename,String keyword,String param1,String param2,String param3,String assertMehtod,String jsonPath,String expected,String result,String actualResponse){
        System.out.println("*********当前执行到第"+rowNO+"行，内容是"+String.format("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s",rowNO,catagory,casename,keyword,param1,param2,param3,assertMehtod,jsonPath,expected,result,actualResponse));
        //是真正的用例行才执行,也就是第一二列去掉空格之后，长度小于1。
        if(keyword.trim().length()>0) {
            List<String>  testContent=new ArrayList<>();
            testContent.add(keyword);
            testContent.add(param1);
            testContent.add(param2);
            testContent.add(param3);
            //运行关键字方法
            boolean runRes = AutoTools.invokeKeyWord(inter, testContent, KEY_COL);
            //运行断言方法，只有写了断言关键字才运行
            if(assertMehtod.trim().length()>0){
                List<String> asssertList=new ArrayList<>();
                asssertList.add(assertMehtod);
                asssertList.add(jsonPath);
                asssertList.add(expected);
                //断言之后获取的返回结果
                runRes = AutoTools.invokeKeyWord(inter, asssertList, ASSERT_COL);
            }

            int rowInt = Integer.parseInt(rowNO);
            //写入返回结果
            if(runRes){
                results.writeCell(rowInt,10,"PASS");
                results.writeCell(rowInt,11,inter.getResult());
            }else{
                results.writeFailCell(rowInt,10,"FAIL");
                results.writeFailCell(rowInt,11,inter.getResult());
            }

            //断言，让testng知道执行成功还是失败。
            Assert.assertTrue(runRes);
        }
    }

    @DataProvider
    public Object[][] interData(){
        cases.useSheet("登录接口");
        results.useSheet("登录接口");
        return cases.readAsMatrix();
    }

    @AfterClass
    public void tearDown(){
        cases.close();
        results.save();
        Report.sendreport("Cases\\Result\\resultofInterNG"+runDate+".xlsx",runDate);
    }


}
